package com.oxygen.design.singleton;

import com.oxygen.design.singleton.bean.*;

/**
 * @author wangjie
 * @title: SingletonPatternDemo
 * @description: TODO
 * @date 2022/3/30 10:03
 **/
public class SingletonPatternDemo {

    public static void main(String[] args) {

        // 不合法的构造函数
        // 编译时错误：构造函数 SingleObject() 是不可见的
        //SingleObject object = new SingleObject();

        // 获取唯一可用的对象
        SingleObject object = SingleObject.getInstance();
        object.showMessage();

        // 懒汉式，线程不安全
        SingleObjectIdlerThreadUnSafety singleObjectIdlerThreadUnSafety = SingleObjectIdlerThreadUnSafety.getInstance();
        singleObjectIdlerThreadUnSafety.showMessage();

        // 懒汉式，线程安全
        SingleObjectIdlerThreadSafety singleObjectIdlerThreadSafety = SingleObjectIdlerThreadSafety.getInstance();
        singleObjectIdlerThreadSafety.showMessage();

        // 饿汉式
        SingleObjectHungry singleObjectHungry = SingleObjectHungry.getInstance();
        singleObjectHungry.showMessage();

        // 双检锁/双重校验锁（DCL，即 double-checked locking）
        SingleObjectDCL singleObjectDCL = SingleObjectDCL.getSingleton();
        singleObjectDCL.showMessage();

        // 登记式/静态内部类
        SingleObjectStatic singleObjectStatic = SingleObjectStatic.getInstance();
        singleObjectStatic.showMessage();

        // 枚举
        SingletonEnum singletonEnum = SingletonEnum.INSTANCE;
        singletonEnum.showMessage();

    }

}
